package com.ca.project.core.Util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * @PACKAGE_NAME: com.ca.project.core.Util
 * @NAME: TimeUtil
 * @USER: ASUS
 * @DATE: 2023/9/4
 * @TIME: 下午 02:29
 **/
public class TimeUtils {

    public static String formatDateTime(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDateTime = sdf.format(date);
        return nowDateTime;
    }

    public static String formatDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDateTime = sdf.format(date);
        return nowDateTime;
    }

    /**
     * 获取当前时间日期
     * @return datetime
     */
    public static String getNowDateTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowDateTime = sdf.format(new Date());
        return nowDateTime;
    }

    /**
     * 获取当前日期
     * @return date
     */
    public static String getNowDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = sdf.format(new Date());
        return nowDate;
    }

    /**
     * 获取当前年月
     * @return date
     */
    public static String getNowMonth(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String nowMonth = sdf.format(new Date());
        return nowMonth;
    }
    /**
     * 获取上个月份
     * @return date
     */
    public static String getLastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(cal.MONTH, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String lastMonth = sdf.format(cal.getTime());
        return lastMonth;
    }

    /**
     * 获取当前日期数字
     */
    public static String getNowDateTimeStr(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String nowDate = sdf.format(new Date());
        return nowDate;
    }

    /**
     * 获取当前日期数字
     */
    public static String getNowDateStr(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowDate = sdf.format(new Date());
        return nowDate;
    }

    /**
     * 判断某天是否在今日之前
     * @param date
     * @return
     */
    public static boolean isBeforeToday(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date today = sdf.parse(getNowDate());
        Date tag = sdf.parse(date);
        if (tag.compareTo(today) < 0) {
            return true;
        }
        return false;
    }

    /**
     * 校验时间是否过期
     * @param time
     * @return
     */
    public static boolean isOverDue(String time){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date today = sdf.parse(TimeUtils.getNowDateTime());
            Date tag = sdf.parse(time);
            if (tag.compareTo(today) < 0) {
                return true;
            }
            return false;
        }catch (ParseException e){
            return false;
        }

    }

    /**
     * 时间加减法
     * */
    public static Date AddDate(Date date,Integer second){
        if(null==date)
            date=new Date();
        Calendar cal=new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.SECOND, second);
        return cal.getTime();

    }


    /**
     * 获取下周7天的日期和星期
     * @return map
     */
    public static List<HashMap<String,String>> getNextSevenDatesAndWeeksMap(){
        List<HashMap<String,String>> weekAndDates = new ArrayList<>();
        String[] weekdays = new String[]{"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        for(int i=1;i<=7;i++){
            HashMap<String,String> map = new HashMap<>();
            map.put("week",weekdays[i-1]);
            map.put("date",String.valueOf(LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.of(i)))));
            weekAndDates.add(map);
        }
        return weekAndDates;
    }


    /**
     * 获取接下来5天的日期
     * @return
     */
    public static List<String> getNextFiveDays(){
        List<String> dates = new ArrayList<>();
        Date date = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        for (int i=1;i<=5;i++){
            dates.add(sdf.format(new Date(date.getTime() + i * 24 * 60 * 60 * 1000)));
        }
        return dates;
    }

    /**
     * 根据日期获得所在周的日期
     * @param
     * @return
     */
    public static List<String> getThisWeek(Date date) {
        int b = date.getDay();
        Date fdate;
        List<String> list = new ArrayList<>();
        Long fTime = date.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a-1, formatDate(fdate));
        }
        return list;
    }

    public static List<HashMap<String,String>> getThisWeekAndDateMap(Date date) {
        String[] weekdays = new String[]{"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        int b = date.getDay();
        Date fdate;
        Long fTime = date.getTime() - b * 24 * 3600000;
        List<HashMap<String,String>> weekAndDates = new ArrayList<>();
        for (int a = 1; a <= 7; a++) {
            HashMap<String,String> map = new HashMap<>();
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            map.put("week",weekdays[a-1]);
            map.put("date",formatDate(fdate));
            weekAndDates.add(map);
        }
        return weekAndDates;
    }

    /**
     * 根据日期获取下周的日期
     * @param
     * @return
     */
    public static List<String> getNextWeek(Date date) {
        List<String> list = new ArrayList<>();
        for (int a = 1; a <= 7; a++) {
            list.add(String.valueOf(LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.of(a)))));
        }
        System.out.println(list);
        return list;
    }

    /**
     * String 类型转 Date
     * @param dateString
     * @return
     * @throws ParseException
     */
    public static Date stringToDate(String dateString) throws ParseException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
        return fmt.parse(dateString);
    }


}
